package com.du.condition;

import com.du.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author djg
 * @date 2020/11/18
 * @des
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata 当前类的注解信息
     * @param registry BeanDefinition注册类，
     *                 把所有需要添加到容器中的bean，调用registry.registerBeanDefinition()手动注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean red = registry.containsBeanDefinition("com.du.bean.Red");
        boolean blue = registry.containsBeanDefinition("com.du.bean.Blue");
        if (red && blue) {
            //指定Bean定义信息，Bean的类型等等
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class);
            //注册一个bean，指定bean的名称
            registry.registerBeanDefinition("rainBow",rootBeanDefinition);
        }
    }
}
